/**
 * Created by Administrator on 2017/2/8.
 */

var footer = false;
var mid = '';
var store_id = '';
$(function () {
    var key = getCookie('key');
    if (!key) {
        window.location.href = WapSiteUrl + '/tmpl/member/login.html';
    }
    function initPage() {
        $.ajax({
            type: 'post',
            url: ApiUrl + "/index.php?act=store&op=my_store",
            data: {key: key},
            dataType: 'json',
            success: function (result) {

                if (result.code == 400) {
                    $.sDialog({
                        skin:"red",
                        content:result.datas.error,
                        okBtn:true,
                        cancelBtn:true,
                        "okBtnText": "开店", //确定按钮的文字
                        "cancelBtnText": "取消", //取消按钮的文字
                        "okFn": function() { window.location.href = WapSiteUrl + '/tmpl/member/my_shop_taocan.html';}, //点击确定按钮执行的函数
                        "cancelFn": function() { window.location.href = WapSiteUrl + '/tmpl/member/member.html';} //点击取消按钮执行的函数
                    });
                    return false;
                }
                get_footer();
                if (result.datas.open_store && result.datas.store_info.length <= 0) {
                    window.location.href = WapSiteUrl + '/tmpl/member/my_store_set.html';
                }
                if (!result.datas.open_store) {
                    window.location.href = WapSiteUrl + '/tmpl/member/my_shop_taocan.html';
                }
                if (result.datas.store_info.length <= 0) {
                    $('#footer').addClass('posa');
                } else {
                    $('#footer').removeClass('posa');
                }
                mid = result.datas.store_info.member_id;
                store_id = result.datas.store_info.store_id;
                addCookie('store_id',result.datas.store_info.store_id, 188);
                addCookie('store_name',result.datas.store_info.store_name, 188);
                var data = result;
                data.WapSiteUrl = WapSiteUrl;//页面地址
                data.ApiUrl = ApiUrl;
                data.key = getCookie('key');
                template.helper('formattime', function (nS) {
                    var d = new Date(parseInt(nS) * 1000);
                    var s = '';
                    s += d.getFullYear() + '年';
                    s += (d.getMonth() + 1) + '月';
                    s += d.getDate() + '日 ';
                    s += d.getHours() + ':';
                    s += d.getMinutes();
                    return s;
                });
                template.helper('p2f', function (s) {
                    return (parseFloat(s) || 0).toFixed(2);
                });
                template.helper('parseInt', function (s) {
                    return parseInt(s);
                });
                template.helper('avatar', function (s) {
                    return 'http://'+window.location.host+'/data/upload/shop/store/'+s;
                });
                var html = template.render('header-info-tpl', data);
                $(".header_info").append(html);
            }
        });
    }

//初始化页面
    initPage();
});
function get_footer() {
    if (!footer) {
        footer = true;
        $.ajax({
            url: WapSiteUrl + '/js/tmpl/footer.js',
            dataType: "script"
        });
    }
}

function share(){
     window.location.href = 'shop_qrcode.html?mid='+mid+'&store_id='+store_id;
}